$(function(){
//新COMM
//搜索框
var $hdText, isSearchKey, curKeyWords,daykwords,daykwordsUrl;
$hdText=$('#hd-search .hd-text');
//判断当前页面的搜索关键字
isSearchKey=location.href.indexOf("search.m18.com");
curKeyWords=$("body").attr("data-keywords");
daykwords=$("#day-keywords").attr("name");
daykwordsUrl=$("#day-keywords").val();
if(isSearchKey!=-1&&curKeyWords!=""){
    $hdText.val(curKeyWords);
}
else{
    $hdText.val(daykwords);
}
$hdText.focus(function(){
    $(this).select();
    $(this).removeClass('c9');
    if($(this).val()==this.defaultValue){
        $(this).val('');
        }
    }).blur(function(){
        if($(this).val()==''){
            $(this).addClass('c9').val(this.defaultValue);
            }
        });
$('#hd-search form:first').submit(function(){
    if($hdText.val()==$hdText[0].defaultValue||$hdText.val()==''){
        return false;
        }else if($hdText.val()===daykwords){
        window.open(daykwordsUrl);
        return false;
        }
    }); 
//auto complete
var ac_options = {
        autoSubmit: true,
        serviceUrl: 'http://search.m18.com/PredictService.aspx',
        width: 308,
        deferRequestBy: 200, //miliseconds
        params: { app: 'prediction' },
        noCache: false //set to true, to disable caching
    };
$('#ht-autosearch').autocomplete(ac_options);
//导航菜单
var $menuList=$("#hd-nav li"),$menuContent=$("#menuItem").find(".mdetail"),$menuContainer=$("#menuItem"),$winWidth=($(window).width()>=980) ? $(window).width() : 980;
    var hdmenu={
        position:function(winWidth){
            $menuContainer.css("left",((winWidth-980)/2)+87);
            $menuContainer.css("top",$('#hd').height());
        },
        showMenu:function(i){
            var n=i, me=this,showfun;
            showfun=function(){
                if($menuList.eq(n).hasClass("noSubmenu")){
                return false;
            }
                else{
                    $menuList.eq(n).addClass('hover');  
                }
                $menuList.eq(n).siblings().removeClass('hover');
                if($.browser.msie&&$.browser.version==6){
                    $("#selectMask").height($menuContent.eq(n-1).height()+3).width($menuContent.eq(n-1).width()+6);
                }
                $menuContainer.css("visibility","visible");
                $menuContent.css("visibility","hidden");
                $menuContent.eq(n-1).css("visibility","visible");
            }
            me.showMenuTimer=setTimeout(showfun,60)
        },
        hideMenu:function(){
            var me=this,hidefun;
            clearTimeout(me.showMenuTimer);
            hidefun=function(){
                $menuList.removeClass('hover');
                $menuContainer.css("visibility","hidden")
                $menuContent.css("visibility","hidden");
            }
            me.hideMenuTimer=setTimeout(hidefun,60)
        },
        int:function(){
            hdmenu.position($winWidth);
            $menuList.hover(function(){
                var $winWidth=($(window).width()>980) ? $(window).width() : 980;
                hdmenu.position($winWidth);
                var n=$menuList.index($(this));
                hdmenu.showMenu(n)
            },function(){
                hdmenu.hideMenu();  
            })
            $menuContent.hover(function(){
                var n=$menuContent.index($(this));
                hdmenu.showMenu(n+1);
            },function(){
                hdmenu.hideMenu();
            })
        }
    }
    //搜索关键字
    $("#hdsearch-box").append($("#sKeywords"));
    $("#sKeywords").css("visibility","visible")
    //顶部slide
    $("#headSlide-box").append($("#headSlide"));
    $("#headSlide").css("visibility","visible")
    hdmenu.int();

//顶部下拉菜单
$("li.hd-qbar-list-drop").hover(function(){
        $(this).find("ul").show();
    }, function(){
        $(this).find("ul").hide();
    });

//联合登录修改Email

var $hd=$('#hd-login');
if(window.IsShowChangeEmail&&$hd.length>0){
    var changeEmailHtml='<div id="ologin-tips" class="tips tip-t"><div><p><a href="http://www.m18.com/Contact/ContactEmail.aspx" class="a2" target="_blank">设置Email地址</a>，及时了解订单状态，收到礼券、促销信息</p></div><div class="tip"><b></b><i></i></div><a href="javascript:;" class="tips-cls"></a></div>';
    var offset=$hd.offset();
    $(changeEmailHtml).appendTo('body').css({left:offset.left+25,top:offset.top+28,position:'absolute',zIndex:10});
    $('#ologin-tips .tips-cls').click(function(){
        $('#ologin-tips').remove();
        });
    }

//购物车数量
if(window.cartItemSum!==undefined){
    $('#hd-cart-amount').text(cartItemSum);
}
//问卷调查入口
function creatLink(link, txt){
    return '<div class="wrap" style="margin-bottom:-30px"><div style="padding-left:18px;background:url(http://img.m18.com/web/i/comm/ico/ico-talk.gif) no-repeat 0 4px;" class="mt20">您对'+txt+'有意见或建议么？ <a class="a2" href="http://static.m18.com/research/' +link+ '.shtml" target="_blank">请告诉我们</a></div></div>';
}
var regList=/list/i, regSearch=/search/i, regCart=/OrderShopCart/i, locHost=location.hostname, locHref=location.href;
if(regList.test(locHost)&&!window.researchHTML){
    window.researchHTML=creatLink('list', '列表页');
    $('#help3').parent().before(researchHTML);
}else if(regSearch.test(locHost)&&!window.researchHTML){
    window.researchHTML=creatLink('search', '搜索结果');
    $('#help3').parent().before(researchHTML);
}else if(regCart.test(locHref)&&!window.researchHTML){
    window.researchHTML=creatLink('cart', '购物车');
    $('#sft2').before(researchHTML);
}
//add compagin banner
var $banner,$bannerBbs,burl,istuan,isnew,showbanner;
    $banner=$('<div class="wrap980 none b-tempos"><a href="http://www.m18.com/app/AdEntrance.aspx?from=MHF01E&targetURL=http://static.m18.com/dcp/special.shtml" target="_blank"><img height="60" width="980" alt="限时优惠全场满59免运费" style="vertical-align:bottom" src="http://img.m18.com/web/pic/other/1204/0420/all-bar.jpg"></a></div>');
    burl=window.location.href;
    istuan = burl.indexOf("DailyScareBuying.aspx")!=-1;
    isnew  = burl.indexOf("ArrivalAllStyle.htm")!=-1;
    showbanner=istuan||isnew;
    if(!showbanner || $("div.b-tempos").size()!=0){
        return false;
    }
    else{
        if(showbanner && $("div.b-tempos").size()==0){
            $("div.hd-submenu").after($banner);
            $banner.slideDown("1000");
        }
    }
});